﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using System.Linq;

namespace NBA_Feed
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            this.NBA_News = new ObservableCollection<ItemViewModel>();
            this.NBA_com_Personalities = new ObservableCollection<ItemViewModel>();
            this.NBA_Store = new ObservableCollection<ItemViewModel>();
            this.Other_NBA_Feeds = new ObservableCollection<ItemViewModel>();
            this.NBA_Teams = new ObservableCollection<ItemViewModel>();
        }

        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public ObservableCollection<ItemViewModel> NBA_News { get; private set; }
        public ObservableCollection<ItemViewModel> NBA_com_Personalities { get; private set; }
        public ObservableCollection<ItemViewModel> NBA_Store { get; private set; }
        public ObservableCollection<ItemViewModel> Other_NBA_Feeds { get; private set; }
        public ObservableCollection<ItemViewModel> NBA_Teams { get; private set; }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData()
        {
            // Sample data; replace with real data
            this.NBA_News.Add(new ItemViewModel() { RssName = "NBA News (All Feeds)", RssUrl = "http://www.nba.com/rss/nba_rss.xml" });
            this.NBA_News.Add(new ItemViewModel() { RssName = "League Headlines", RssUrl = "http://www.nba.com/aroundtheassociation/rss.xml" });
            this.NBA_News.Add(new ItemViewModel() { RssName = "Power Rankings", RssUrl = "http://www.nba.com/powerrankings/rss.xml" });

            this.NBA_com_Personalities.Add(new ItemViewModel() { RssName = "David Aldridge, TNT Analyst", RssUrl = "http://www.nba.com/rss/david_aldridge.rss" });
            this.NBA_com_Personalities.Add(new ItemViewModel() { RssName = "Steve Aschburner, NBA.com", RssUrl = "http://www.nba.com/rss/steve_aschburner.rss" });
            this.NBA_com_Personalities.Add(new ItemViewModel() { RssName = "Fran Blinebury, NBA.com", RssUrl = "http://www.nba.com/rss/fran_blinebury.rss" });
            this.NBA_com_Personalities.Add(new ItemViewModel() { RssName = "Scott Howard-Cooper, NBA.com", RssUrl = "http://www.nba.com/rss/scott_howard_cooper.rss" });
            this.NBA_com_Personalities.Add(new ItemViewModel() { RssName = "Shaun Powell, NBA.com", RssUrl = "http://www.nba.com/rss/shaun_powell.rss" });
            this.NBA_com_Personalities.Add(new ItemViewModel() { RssName = "John Schuhmann, NBA.com", RssUrl = "http://www.nba.com/rss/john_schuhmann.rss" });

            this.NBA_Store.Add(new ItemViewModel() { RssName = "NBA Store (All Feeds)", RssUrl = "http://feeds.nba.com/nbacourtside?format=xml" });

            this.Other_NBA_Feeds.Add(new ItemViewModel() { RssName = "WNBA", RssUrl = "http://www.wnba.com/rss/wnba_rss.xml" });
            this.Other_NBA_Feeds.Add(new ItemViewModel() { RssName = "NBA D-League", RssUrl = "http://www.nba.com/downlowonthedleague/rss.xml" });

            XDocument xdoc = XDocument.Load("BaseClass/XMLFile1.xml");

            //执行linq检索(xmlLOG)
            foreach (var tr in xdoc.Element("tbody").Elements("tr"))
            {
                var a = tr.Descendants("a").First<XElement>();
                //条件都加在这里,注意条件用双等，多条件用 && 连接
                //用户名是Bin的所有记录
                var team = new ItemViewModel
                        {
                            RssName = a.Value,
                            RssUrl = a.Attribute("href").Value
                        };
                team.RssImage = tr.Descendants("img").First<XElement>().Attribute("src").Value;

                this.NBA_Teams.Add(team);
            }
            
            this.IsDataLoaded = true;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}